package com.base.mine;

import android.content.Intent;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.FileUtil;
import com.base.commonlib.utils.SystemUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.commonlib.widget.DestoryCommonDialog;
import com.base.mine.config.RouteConfig;
import com.base.mine.databinding.ActivitySettingBinding;
import com.base.mine.presenter.SettingPresenter;
import com.base.mine.view.SettingView;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by weikailiang on 2019/5/28.
 */

@Route(path = RouteConfig.ACTIVITY_SETTER)
public class SettingActivity extends CBaseBarMvpActivity<SettingPresenter> implements SettingView {
    protected ActivitySettingBinding mBinding;
    private Disposable mDisposable;
    private Disposable mDisposable_clean;

    @Autowired
    public String userId;
    @Autowired
    public String account;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_setting;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivitySettingBinding) mRootBinding;
        mPresenter=new SettingPresenter(this,this);
    }

    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle("设置");
        Set<String> strings = new HashSet<>();
        mBinding.tvVersion.setText(SystemUtil.packageName(mContext)+"");
        mBinding.llClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clean();
            }
        });
        mBinding.llExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonDialog commonDialog = new CommonDialog(mContext, "确定要退出?");
                commonDialog.setOnPosClick(new CommonDialog.OnPosClick() {
                    @Override
                    public void onClick() {
//                        SpUtil.setToken("");
//                        ARouter.getInstance().build(RouteConfig.ACTIVITIY_LOGIN).navigation();
//
                        mPresenter.loginOut(userId);
                    }
                });
                commonDialog.show();
            }
        });


        mBinding.llDestroy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DestoryCommonDialog commonDialog = new DestoryCommonDialog(mContext, "\t确定要注销账户?\n\t注销账户后积分会清零！");
                commonDialog.setOnPosClick(new DestoryCommonDialog.OnPosClick() {
                    @Override
                    public void onClick() {
                        mPresenter.destroy();
                    }
                });
                commonDialog.show();
            }
        });

        mBinding.llTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_DATE_SETTINGS);
                startActivity(intent);
            }
        });
        mBinding.llBluetooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivity(intent);
            }
        });
        mBinding.llDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ARouter.getInstance().build("/sweb/common")
//                        .withInt("key",1)
//                        .withString("url","file:///android_asset/admin.html")
//                        .withString("title","江苏中越科技")
//                        .withString("name","测试")
//                        .navigation();
                ARouter.getInstance().build("/sweb/googlevr").navigation();
            }
        });
        mBinding.llChangepw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/changepw/pw").navigation();
            }
        });
        mBinding.llVodeo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/video/vodeosetting").navigation();
            }
        });
    }
    private void clean()
    {
        showPb();
        if(mDisposable_clean!=null&&!mDisposable_clean.isDisposed())
        {
            mDisposable_clean.dispose();
        }
        Observable<Object> observable = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                File temp = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + RouteConfig.TEMPPATH);
                long dirLength = FileUtil.getDirLength(BaseApp.sAppContext.getCacheDir());
                long dirLength1 = FileUtil.getDirLength(temp);
                boolean b = FileUtil.deleteFilesInDir(BaseApp.sAppContext.getCacheDir());
                boolean b1 = FileUtil.deleteFilesInDir(temp);
                if(b&&b1)
                {
                    String memorySize = FileUtil.byte2FitMemorySize(dirLength + dirLength1);
                    emitter.onNext(memorySize);
                }else
                {
                    throw new Exception();
                }

            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());

        mDisposable_clean = observable.subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                hidePb();
                showMes("缓存已清理 "+o.toString());

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                hidePb();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mDisposable!=null&&!mDisposable.isDisposed())
        {
            mDisposable.dispose();
        }
        if(mDisposable_clean!=null&&!mDisposable_clean.isDisposed())
        {
            mDisposable_clean.dispose();
        }
    }

    @Override
    public void returnDestroy(String msg) {
        ToastUtil.show("注销成功");
        SpUtil.setToken("");
        Set<String> strings = new HashSet<>();
        strings = SpUtil.getStringSet(strings);
        Iterator<String> it_b=strings.iterator();
        while(it_b.hasNext()){
            String a=it_b.next();
            if (a.contains(account)) {
                it_b.remove();
            }
        }

        SpUtil.setUserAccount("");
        SpUtil.setUserPw("");
        ARouter.getInstance().build(RouteConfig.ACTIVITIY_LOGIN).navigation();
        BaseApp.clearActivity();
    }

    @Override
    public void returnLoginOut(String msg) {
        ToastUtil.show("退出成功");
        SpUtil.setToken("");
        ARouter.getInstance().build("/mine/pwdlogin").navigation();
        BaseApp.clearActivity();
    }
}
